package com.gome.axwl.aoc.web.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.anxl.sso.client.api.AccountInfo;
import com.anxl.sso.client.api.Dc;
import com.anxl.sso.client.api.Token;
import com.anxl.sso.client.support.AnxlSsoSupport;
import com.gome.axwl.aoc.common.constants.LoginConstant;
import com.gome.axwl.aoc.entity.dto.QueryAllotTaskDto;
import com.gome.axwl.aoc.entity.dto.TaskJobSearchDto;
import com.gome.axwl.aoc.entity.dto.TruckListSearchDto;
import com.gome.axwl.aoc.facade.TruckFacade;
import com.gome.axwl.aoc.service.sso.ServerGomeSsoClientService;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 项目名称：gome-axwl-aoc-web
 * 类名称：TruckController
 * 类描述：
 * 创建人：chengh
 * 创建时间：2018年4月24日16:59:06
 */
@Api(tags = "车次查询")
@RestController
public class TruckController {

    private final static Logger log = LoggerFactory.getLogger(TruckController.class);

    @Autowired
    TruckFacade truckFacade;

    @Autowired
    private ServerGomeSsoClientService ssoClientService;

    @ApiOperation(value = "模糊查询司机列表", notes = "development by chengh")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "查询关键字", paramType = "query", dataType = "String", required = true)
    })
    @RequestMapping(value = "driver/queryDriver", method = RequestMethod.POST)
    public Object getDriverByKeyword(@RequestBody Map<String, String> map, HttpServletRequest request, HttpServletResponse response) {
        AccountInfo accountInfo = (AccountInfo) request.getSession().getAttribute(LoginConstant.LOGIN_USER);
        if (accountInfo == null) {
            Token token = (Token) request.getSession().getAttribute(AnxlSsoSupport.SSO_CONTEXT);
            accountInfo = ssoClientService.getUserByLocalUserId(token.getUid(), request, response);
        }
        List<Dc> dcList = accountInfo.getDcs();         //dc集合
        StringBuffer dcId = new StringBuffer();
        String dcIdString;
        if (dcList != null && dcList.size() > 0) {
            for (int i = 0; i < dcList.size(); i++) {
                dcId.append(",").append(dcList.get(i).getDcId());
            }

            if (dcId.length() > 0) {
                dcIdString = dcId.toString().substring(1, dcId.length());
            } else {
                dcIdString = "0";
            }
        } else {
            dcIdString = "0";
        }
        log.info("调用TruckController.getDriverByKeyword(模糊查询司机列表),dcid列表{},参数为:{}", dcIdString, map.toString());
        return truckFacade.getDriverByKeyword(map.get("keyword"), dcIdString);
    }

    @ApiOperation(value = "查询车次列表", notes = "development by chengh")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "takesendNo", value = "车次号", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "waybillNo", value = "运单号", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "orderNo", value = "订单号", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "takesendTaskNo", value = "任务单号\t", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "driver", value = "司机", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "vehicleNo", value = "车牌号", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "status", value = "状态", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageNum", value = "当前页", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query", dataType = "int", required = true)
    })
    @RequestMapping(value = "truck/getTruckList", method = RequestMethod.POST)
    public Object getTruckList(@RequestBody TruckListSearchDto truckListSearchDto, HttpServletRequest request, HttpServletResponse response) {
        log.info("调用TruckController.truckListSearchDto(查询车次列表),参数为:" + truckListSearchDto.toString());
        AccountInfo accountInfo = (AccountInfo) request.getSession().getAttribute(LoginConstant.LOGIN_USER);
        if (accountInfo == null) {
            Token token = (Token) request.getSession().getAttribute(AnxlSsoSupport.SSO_CONTEXT);
            accountInfo = ssoClientService.getUserByLocalUserId(token.getUid(), request, response);
        }
        List<Dc> dcList = accountInfo.getDcs();         //dc集合
        if (dcList != null && dcList.size() > 0) {
            Integer[] acceptDcIds = new Integer[dcList.size()];
            for (int i = 0; i < dcList.size(); i++) {
                acceptDcIds[i] = dcList.get(i).getDcId();
            }
            truckListSearchDto.setAcceptDcIds(acceptDcIds);
        } else {          //用户没有dc
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list", null);
            jsonObject.put("pageNum", 1);
            jsonObject.put("pageSize", truckListSearchDto.getPageSize());
            jsonObject.put("pages", 0);
            jsonObject.put("size", 0);
            jsonObject.put("total", 0);
            return new ResultData(ErrorCode.SUCCESS, ErrorCode.SUCCESS_MSG, jsonObject);
        }
        log.info("登录用户:{},session中DC权限为:{},查询的DC为:{}", accountInfo.getAccountNo(), dcList.toString(), truckListSearchDto.getAcceptDcIds());
        return truckFacade.getTruckList(truckListSearchDto);
    }

    @ApiOperation(value = "查询任务单列表", notes = "development by chengh")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "takesendId", value = "车次id", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "taskNo", value = "任务单号", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "waybillNo", value = "运单号", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "status", value = "状态", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageNum", value = "当前页", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query", dataType = "int", required = true)
    })
    @RequestMapping(value = "taskJob/getTaskJobList", method = RequestMethod.POST)
    public Object getTaskJobList(@RequestBody TaskJobSearchDto taskJobSearchDto, HttpServletRequest request) {
        log.info("调用TruckController.getTaskJobList(查询任务单列表),参数为:" + taskJobSearchDto.toString());
        return truckFacade.getTaskJobList(taskJobSearchDto);
    }

    /**
     * 下载语音评价文件
     *
     * @param taskNo
     */
    @RequestMapping(value = "takesendTask/filesDownloadToZip")
    public ResultData filesDownloadToZip(String taskNo, HttpServletResponse response) {
        log.info("调用TruckController.filesDownloadToZip(下载语音评价zip),参数为:" + taskNo);
        return truckFacade.filesDownloadToZip(taskNo, response);
    }



    @ApiOperation(value = "任务单查询", notes = "development by John-Li")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "takesendNo", value = "车次号 ", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "takesendId", value = "车次id", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "taskNo", value = "任务单号", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "waybillNo", value = "运单号", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "status", value = "状态", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageNum", value = "当前页", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query", dataType = "int", required = true)
    })
    @RequestMapping(value = "takesendTask/queryTakesendTask", method = RequestMethod.POST)
    public Object queryAllotTask(@RequestBody QueryAllotTaskDto queryTakesendTask, HttpServletRequest request) {
        log.info("调用TruckController.queryTakesendTask(查询任务单列表),参数为:" + queryTakesendTask.toString());
        return truckFacade.queryTakesendTask(queryTakesendTask);
    }

    @ApiOperation(value = "调拨和运输详情", notes = "development by John-Li")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "takesendId", value = "车次id", paramType = "query", dataType = "String", required = true)
    })
    @RequestMapping(value = "takesend/queryTakesendDetail", method = RequestMethod.POST)
    public Object queryTakesendDetail(@RequestBody TaskJobSearchDto taskJobSearchDto, HttpServletRequest request) {
        log.info("调用TruckController.queryTakesendDetail(查询任务单列表),参数为:" + taskJobSearchDto.getTakesendId());
        return truckFacade.queryTakesendDetail(taskJobSearchDto.getTakesendId());
    }


}
